package floyd;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;

public class Dyadic {
	public static int[][] init(Set<Matrix> set) {
		int size=size(set);
		
		int[][] matrixs=new int[size][size];
				
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
//				if(matrixs[j][i]!=Integer.MAX_VALUE){
//					matrixs[i][j]=matrixs[j][i];
//					continue;
//				}
				Matrix temp=new Matrix(i, j);
				if(set.contains(temp)){
					matrixs[i][j]=select(temp, set);
				}else{
					matrixs[i][j]=Integer.MAX_VALUE;					
				}
			}
		}
		
		return matrixs;
	}
	
	private static int select(Matrix temp,Set<Matrix> set) {
		for(Matrix matrix : set){
		  if(temp.equals(matrix)){
			  return matrix.getDistance();
		  }
		}
		return Integer.MAX_VALUE;
	}
	
	private static int size(Set<Matrix> set){
		Collection<Integer> xlist=CollectionUtils.collect(set, new Transformer() {
			public Object transform(Object input) {
				return ((Matrix)input).getX();
			}
		});
		Collection<Integer> ylist=CollectionUtils.collect(set, new Transformer() {
			public Object transform(Object input) {
				return ((Matrix)input).getY();
			}
		});
		
		return Collections.max(Arrays.asList(Collections.max(xlist),Collections.max(ylist)))+1;
	}
	
	public static void draw(int size,int[][] dist){
		System.out.println("\n");
		for(int i = 0; i < size; i++){
			System.out.println();
			for(int j = 0; j < size; j++){
				System.out.print(dist[i][j] + " | ");
			}
		}
		System.out.println("\n");
	}
}
